﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Configuration;
namespace Web.Admin
{
    public partial class coupon_detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["id"] == null)
            {
                Response.Redirect("Corp/corp_list.aspx");
            }
            else
            {
                odsCoupons.InsertParameters["corpID"].DefaultValue = Request["id"].ToString();
                aback.HRef = string.Format("coupon_list.aspx?id={0}&title={1}", Request["id"].ToString(), Request["title"].ToString());
                lblCorpName.Text = Request["title"].ToString();
            }

        }

        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            Regex reg = new Regex(ConfigurationManager.AppSettings["ImageReg"].ToString());
            HttpFileCollection files = Request.Files;
            string fileExtension = System.IO.Path.GetExtension(files[0].FileName.ToLower());
            if (reg.IsMatch(fileExtension))
            {
                if (files[0].ContentLength / 1024 > 1100)
                {
                    Response.Write("<script>alert('文件大小不能超过1MB,请重新上传!')</script>");
                    return;
                }
                else
                {
                    string floderPath = string.Format("/UploadFile/Coupons/{0}", Request["id"].ToString());
                    if (Web.AppCode.Sys_Common.CreatePath(floderPath))
                    {
                        string relativePath = floderPath + "/" + Guid.NewGuid().ToString() + fileExtension;
                        string filePath = HttpContext.Current.Server.MapPath(string.Format("{0}", relativePath));
                        files[0].SaveAs(filePath);
                        odsCoupons.InsertParameters["imageSrc"].DefaultValue = relativePath;
                    }
                    else
                    {
                        Response.Write("<script>alert('保存上传图片时，创建文件路径失败!')</script>");

                    }
                }


            }
            else
            {
                Response.Write("<script>alert('不支持此格式上传!')</script>");
            }
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
                Response.Write("<script>alert('未能添加成功，请稍候再试！')</script>");
                //Response.Write("<script>alert('"+e.Exception.Message+"')</script>");
            }
            else
            {
                Response.Write("<script>alert('添加成功')</script>");
            }
        }

    }
}
